import Vue from "vue";
import Vuex from "vuex";

Vue.use(Vuex);

const types = {
  SET_USER: "SET_USER",
  SET_IS_AUTHENTICATED: "SET_IS_AUTHENTICATED",
};

const state = {
  user: null,
  isAuthenticated: false,
};

const getters = {
  isAuthenticated: (state) => state.isAuthenticated,
  user: (state) => state.user,
};

const mutations = {
  [types.SET_USER](state, user) {
    state.user = user;
  },
  [types.SET_IS_AUTHENTICATED](state, isAuthenticated) {
    state.isAuthenticated = isAuthenticated;
  },
};

const actions = {
  setUser({ commit }, user) {
    commit(types.SET_USER, user);
  },
  setIsAuthenticated({ commit }, isAuthenticated) {
    commit(types.SET_IS_AUTHENTICATED, isAuthenticated);
  },
  clearCurrentState({ commit }) {
    commit(types.SET_USER, null);
    commit(types.SET_IS_AUTHENTICATED, false);
  },
};

const modules = {};

export default new Vuex.Store({
  state,
  getters,
  mutations,
  actions,
  modules,
});
